/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layoutlang;

import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.ListIterator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import org.xml.sax.Attributes;

import org.xml.sax.helpers.XMLFilterImpl;

/**
 *
 * @author rejun001
 */
/** 
  * This shows how to parse an XML File using SAX
  * and display the same information from the xml file by 
  * implementing the ContentHandler interface
  */
public class LayoutLangJAXBTest1 {
    public static void main (String args[]) {
        
        try {
            JAXBContext jc = JAXBContext.newInstance("layoutlang");
            Unmarshaller um = jc.createUnmarshaller();
            EmrBands bands = (EmrBands) um.unmarshal(new File("ENCAPSULATED_SFO.xml"));
            ObjectFactory of = new ObjectFactory();
            String formTitle = bands.getFormTitle();
            String formNumber = bands.getFormNumber();
            System.out.println("formtitle is " + formTitle + ", formNumber is " + formNumber);
            
            List<EmrBandType> bandList = bands.getEmrBand();
            
            ListIterator<EmrBandType> it = bandList.listIterator();
            while (it.hasNext()) {
                EmrBandType band = it.next();
                String seqId = band.getSequenceId();
                System.out.println("seqId is " + seqId);
                if (band.isCanStartPage()) {
                    System.out.println("can start page");
                }
                else {
                    System.out.println("no can do");
                }
                
//                Serializable rectser =  band.getContent().get(0);
//                //JAXBElement<EmrRectsType> JErects1 =  of.createEmrBandTypeEmrRects(contentRects);
//                //EmrRectsType rects = JErects1.getValue();
//                EmrRectsType rects = (EmrRectsType) rectser;
//
//                
//                System.out.println("Band: rects1 content-type: " + rects.getContentType());
        
               
            }
            System.out.println("finished it loop");
            
        } catch (Exception je) {
            je.printStackTrace();
        }
        

//        FileInputStream instream = null;
//        InputSource is=null;
//	try {
//	    SAXParserFactory spf = SAXParserFactory.newInstance();
//	    spf.setNamespaceAware(true);
//	    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
//
//	    xmlReader.setContentHandler(new MyContentHandler());
//
//            instream = new FileInputStream("books.xml");
//            is = new InputSource(instream);
//
//	    xmlReader.parse(is);
//	} catch( IOException e) {
//	    e.printStackTrace();
//	    System.exit(0);
//	} catch( SAXException e) {
//	    e.printStackTrace();
//	    System.exit(0);
//	} catch( ParserConfigurationException e) {
//	    e.printStackTrace();
//	    System.exit(0);
//	}

    }
}

class MyContentHandler extends XMLFilterImpl {
    boolean printMe = false;
    static int authorCount=0;

    private boolean getPrintValue() {
        return printMe;
    }
    private void  setPrintValue(boolean printValue) {
        printMe = printValue;
    }

    public void characters(char[] ch, int start, int length) {
        String gotString = new String(ch, start, length);
        if (printMe) {
           if (! gotString.trim().equals("")) {
               System.out.println( gotString.trim());
           }
        }
    }

    public void startElement(String namespaceURI, String localName,
        String qName, Attributes attributes) {
     
        if (localName.equals("book")) {
           authorCount=0;
           System.out.println( "\nBook Details");
           int attrsLength = attributes.getLength();
           for (int i = 0 ; i < attrsLength ; i ++ ) {
                String  itemId= attributes.getValue(i);
                System.out.println("ItemId " + itemId);

           }
        } 

        if (localName.equals("ISBN") ||
           localName.equals("price") ||
           localName.equals("description") ||
           localName.equals("Discount") ||
           localName.equals("bookCategory") ||
           localName.equals("name")) {

           System.out.println("Book " + localName +": ");
           printMe=true;

        } else if (localName.equals("authorName") ){
             System.out.println("Book " + localName +": ");
             authorCount++; 
             printMe=true;
           
        } else 

            printMe=false;

      
    }

    public void endElement(String namespaceURI,String localName,String qName){
        if (localName.equals("authors")) 
           System.out.println("No of authors :"+authorCount);
    }
}
